﻿using Aweber.Entity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Aweber;
using System.Collections.Generic;
using System.Configuration;
using System.Threading;

namespace IntegrationTests
{


    /// <summary>
    ///This is a test class for BaseCollectionTest and is intended
    ///to contain all BaseCollectionTest Unit Tests
    ///</summary>
    [TestClass()]
    public class BaseCollectionTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        /// Will get a subscriber collection
        /// </summary>
        [TestMethod()]
        public void SubscriberCollectionTest()
        {
            string appId = ConfigurationManager.AppSettings["AweberAppId"];
            API api = new API(appId);
            api.process_appId_token(ConfigurationManager.AppSettings["AweberToken"]);

            Account target = api.getAccount();

            BaseCollection<List> lists = target.lists();

            BaseCollection<Subscriber> subscribers = lists.entries[2].subscribers();

            Int32 totalCount = 0;
            totalCount += subscribers.entries.Count;
            while (!String.IsNullOrEmpty(subscribers.next_collection_link))
            {
                Thread.Sleep(3000);
                subscribers = lists.entries[2].subscribers(subscribers.next_collection_link);
                totalCount += subscribers.entries.Count;
            }

            Assert.AreEqual(totalCount, subscribers.total_size);

        }

        [TestMethod()]
        public void ListCollectionTest()
        {
            string appId = ConfigurationManager.AppSettings["AweberAppId"];
            API api = new API(appId);
            api.process_appId_token(ConfigurationManager.AppSettings["AweberToken"]);

            Account target = api.getAccount();

            BaseCollection<List> lists = target.lists();

            Int32 totalCount = 0;
            totalCount += lists.entries.Count;

            while (!String.IsNullOrEmpty(lists.next_collection_link))
            {
                lists = target.lists(lists.next_collection_link);
                totalCount += lists.entries.Count;
            }

            Assert.AreEqual(totalCount, lists.total_size);
        }

        [TestMethod()]
        public void CampaignCollectionTest()
        {
            string appId = ConfigurationManager.AppSettings["AweberAppId"];
            API api = new API(appId);
            api.process_appId_token(ConfigurationManager.AppSettings["AweberToken"]);

            Account target = api.getAccount();

            BaseCollection<List> lists = target.lists();

            BaseCollection<Campaign> campaigns = lists.entries[2].campaigns();

            Int32 totalCount = 0;
            totalCount += campaigns.entries.Count;
            while (!String.IsNullOrEmpty(campaigns.next_collection_link))
            {
                campaigns = lists.entries[2].campaigns(campaigns.next_collection_link);
                totalCount += campaigns.entries.Count;
            }

            Assert.AreEqual(totalCount, campaigns.total_size);

        }


    }
}
